/**
 * 用户表单验证
 * author yanghj 2014.5.13
 */
var userValidate = {
    
    setPayPWFlag : false,
    resentEmailSec : 0,
    resentMobileSec : 0,
    emailAddr : false,
    emailUrl : false,
        
    //检查新支付密码强度
    chkPWPower : function(pw){
        if(pw==undefined||'object'==typeof(pw)){
            var pw = $('#password').val();
        }
        var score = rulePW.getPowerScore(pw);
        if(score==1){
            userValidate.lightUpPower('low');
            $('#password_level').val(1);
        }else if(score==2||score==3){
            
            userValidate.lightUpPower('middle');
            $('#password_level').val(2);
        }else if(score>=4){
            userValidate.lightUpPower('strong');
            $('#password_level').val(3);
        }else{
            userValidate.lightOffPower();
            userValidate.setPayPWFlag = false;
        }
        userValidate.setPayPWFlag = true;
    },
    
    //点亮密码强度
    lightUpPower : function(id){
        userValidate.lightOffPower();
        $('#'+id).addClass('current');
    },
    //清除点亮密码强度
    lightOffPower : function(){
        $('#level').children('span').removeClass('current');
    },
    
    //获取email地址
    getEmail : function(email){
        if(email){
            userValidate.emailAddr = email;
        }
        else{
            if($('#email').val()==undefined){return;}
            userValidate.emailAddr = $('#email').val();
        }
        
    },
    //获取email网址
    setEmailUrl : function(email){
        userValidate.getEmail(email);
        if(!userValidate.emailAddr){return false;}
        userValidate.emailUrl = emailLib.getEmailUrl(userValidate.emailAddr);
        if(userValidate.emailUrl[2])
        {
            $('#gotoemail').attr('href',userValidate.emailUrl[2]);
        }
        else
        {
            $('#gotoemail').attr("href",'javascript:;');
            $('#gotoemail').attr("target",'_self');
        }
        
    },
    //邮箱_计算重新发送时间
    timingResetEmailCaptcha : function(){
        userValidate.resentEmailSec--;
        if(userValidate.resentEmailSec<0){
            $('#send_submit').attr("disabled", false);
            $('#send_submit').val('发送验证链接');
            $('#gotoemail').hide();
        }else{
            $('#send_submit').attr("disabled", true);
            $('#send_submit').val(userValidate.resentEmailSec+' 秒后重新发送');
            setTimeout(userValidate.timingResetEmailCaptcha,1000);
        }
    },
    //发送邮箱
    sendEmail : function(email,method){       
        var eamilSentStatu = false;
        var from = userValidate.getQueryString("from");
        $.ajax({
            url:'index.php?app=send_email&act=ajax_send_email&from='+from,
            async:false,
            type:"post",
            dataType:"json",
            data:'method='+method+'&email='+email,
            success:function(data){
                if(!data.statu){
                    eamilSentStatu = false;
                    alert(data.msg);
                }else{
                    eamilSentStatu = true;
                }
            }
        });
        if(eamilSentStatu){
            userValidate.resentEmailSec = 120;
            userValidate.setEmailUrl(email);
            $('#gotoemail').show();
            userValidate.timingResetEmailCaptcha();
        }
    },
    
     //手机_计算重新发送时间
    timingResetMobileCaptcha : function(){
        userValidate.resentMobileSec--;
        if(userValidate.resentMobileSec<0){            
            $('#send_mobile_code').attr("disabled", false);
            $('#mobile_verification_code').val('');
            $('#send_mobile_code').val('获取短信验证码');
            $('#show_error').hide();
        }else{
            $('#send_mobile_code').val(userValidate.resentMobileSec+' 秒后重新发送').attr("disabled", true);
            setTimeout(userValidate.timingResetMobileCaptcha,1000);
        }
    },
    //发送手机验证码
    sendMobile : function(mobile,method){
        
        var mobileSentStatu = false;       
        $.ajax({
            url:'index.php?app=send_mobile&act=ajax_send_mobile',
            async:false,
            type:"post",
            dataType:"json",
            data:'method='+method+'&mobile='+mobile,
            success:function(data){
                if(!data.statu){
                    mobileSentStatu = false;
                    alert(data.msg);
                }else{
                    mobileSentStatu = true;
                    $('#show_error').show().html(data.msg);
                }
            }
        });
        if(mobileSentStatu){
            userValidate.resentMobileSec = 120;
            $('#mobile_verification_code').attr("disabled", false);
            $('#send_submit').attr("disabled", false).addClass('submit_cur');           
            userValidate.timingResetMobileCaptcha();
        }
    },
    
    //设置Mobile Session
    setMobileSession : function(mobile){
        $.ajax({
            url:'index.php?app=send_mobile&act=set_mobile_session',
            async:false,
            type:"post",
            dataType:"json",
            data:'phone_mob='+mobile,
            success:function(data){
                return data;
            }
        });
        
    },
    
    //检测邮箱号码
    chackEmail : function(email){
          var reg = /^[\w-.]+@[\w-.]+\.[\w]{1,5}$/i;
          return reg.test(email);
    },
    //检测邮箱号码
    chacMobile : function(mobile){
          // var reg = /^1(3|5|4|7|8)\d{9}$|^[1,5,6,9](?:\d{7}|\d{8}|\d{12})$/;
          var reg = /^1(3|5|4|7|8)\d{9}$/;
          return reg.test(mobile);
              
    },
    //检测邮箱号码
    chacCode : function(code){
          var reg = /^[0-9]{6}$/;
          return reg.test(code);
              
    },

   getQueryString : function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
   }
    
    
    
    
}

/**
 * 密码规则检验
*/
rulePW = {
    getPowerScore : function(value){
            if(value=='')
            {
                return -1;
            }
            if(value.length<6)
            {
                return -2;
            }
            if (value.length > 20)
            {
                return -3;
            }
            var score = 0;
            var verify =/^[0-9A-Za-z|_|\-|+|=|*|!|@|#|$|%|^|&|(|)|~]*$/;
            if(!verify.test(value)){
                return score;
            }
            if(/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)|~]+/.test(value)){
               score++;
            }
            if(/[0-9]+/.test(value)){
               score++;
            }
            if(/[a-z]+/.test(value)){
               score++;
            }
            if(/[A-Z]+/.test(value)){
               score++;
            }
            if(score>1&&(value.length>=9))
            {
               score++;
            }
            return score;
    }
}


/**
 * 邮箱操作类
 * author junlin
 */ 
emailLib = {
    famous_mail_suffix : new Array('163.com','qq.com','tom.com',
                                    'sohu.com','126.com','sina.com','sogou.com','hotmail.com','live.com',
                                    'live.cn','189.cn','foxmail.com','yahoo.com','yahoo.cn','yeah.net',
                                    'wo.com.cn','263.net','21cn.com','eyou.com','188.com','139.com','gmail.com'),
    famous_mail_servers : new Array('mail.163.com', 'mail.qq.com','mail.tom.com',
                                    'mail.sohu.com','www.126.com','sina.com.cn','mail.sogou.com','www.hotmail.com','www.hotmail.com',
                                    'www.hotmail.com','www.189.cn','www.foxmail.com','mail.cn.yahoo.com','mail.cn.yahoo.com','www.yeah.net',
                                    'mail.wo.com.cn','www.263.net','mail.21cn.com','www.eyou.com','www.188.com','mail.10086.cn','gmail.google.com'),
    //获取邮箱网址
    getEmailUrl : function(email){        
        var email_server = '';
        var email_http = '';
        var res = [];
        for (x in emailLib.famous_mail_suffix)
        {
            var search_temp = '@' + emailLib.famous_mail_suffix[x];
            var search_temp_2 = '.' + emailLib.famous_mail_suffix[x];
            if(email.indexOf(search_temp) > 0 || email.indexOf(search_temp_2) > 0) {
                email_server = emailLib.famous_mail_suffix[x];
                email_http = 'http://' + emailLib.famous_mail_servers[x];
                break;
            }
        }
        res.push(email);
        res.push(email_server);
        res.push(email_http);
        return res;
    }
}
